/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccastestore.servlet;

import it.unitn.science.matr155863_148346_145963.bccastestore.DBManager;
import it.unitn.science.matr155863_148346_145963.bccastestore.items.Asta;
import it.unitn.science.matr155863_148346_145963.bccastestore.items.Offer;
import it.unitn.science.matr155863_148346_145963.bccastestore.items.User;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author berto
 */

public class ShowOffersSRV extends HttpServlet {

    private DBManager db=null;
    private static Logger log = Logger.getLogger(ShowOffersSRV.class);
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        try {
          db=(DBManager)super.getServletContext().getAttribute("db_manager");
        }catch(Exception e) {
            log.fatal(e);
            throw new UnavailableException(e.toString());
        }
    }
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String address="/contents/public/errorPage.jsp";
        try {
            List<Offer> offers;
            if(request.getParameter("asta_id")!=null){
                Integer id=Integer.parseInt(request.getParameter("asta_id"));
                Asta asta=db.getAsta(id);
                User user=((User)request.getSession().getAttribute("user"));
                if(!((user.isAdmin())|| //Se e' l'amministratore
                   (asta.getAnnulled())||//L'asta e' stata annullata
                   (asta.getDate().getTime()<new Date().getTime()) //l'asta non e' piu' attiva
                   )){
                    throw new Exception("You don't have the permission to see it");
                }
                if(asta!=null){
                    offers=db.getOfferOfAsta(id);
                    request.setAttribute("offers", offers);
                    request.setAttribute("title", "Offer of asta numer "+asta.getId());
                    address="/contents/user/offersMade.jsp";
                }else{
                    //Product not found
                    request.setAttribute("error_message", "Asta not found");
                }
            }else if(request.getParameter("user_id")!=null){
                Integer id=Integer.parseInt(request.getParameter("user_id"));
                User user=db.getUser(id);
                if(user!=null){
                    offers=db.getOfferOfUser(id);
                    request.setAttribute("offers", offers);
                    request.setAttribute("title", "Offer of user "+user.getUsername());
                    address="/contents/user/offersMade.jsp";
                }else{
                    //Product not found
                    request.setAttribute("error_message", "User not found");
                }
            }else{
                //No product defined
                request.setAttribute("error_message", "No asta or user defined");
            }
        }catch(Exception ex){
            request.setAttribute("error_message", ex.getMessage());
        }finally{
                RequestDispatcher dispatcher=request.getRequestDispatcher(address);
                dispatcher.include(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
